package nmp.gui;

import nmp.lib.CNmpCmd.NmpWifiInfo;
import nmp.remote.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
	
public class CWifiConfigDlg {
	
	private NmpWifiInfo mWifiItem;
	private Context mContext;
	private boolean mSsidEnable;
	private boolean mProtectEnable;
	private boolean mPasswordEenable;
	private String[] protect_str = new String[]{"WPA","WEP","NONE"};
	
	public CWifiConfigDlg(Context context){
		mWifiItem = new NmpWifiInfo();
		mContext = context;
		mSsidEnable = true;
		mProtectEnable = true;
		mPasswordEenable = true;		
	}
	
	public CWifiConfigDlg(Context context,NmpWifiInfo item){
		mWifiItem = item;
		mContext = context;
		mSsidEnable = true;
		mProtectEnable = true;
		mPasswordEenable = true;			
	}
	
	public void SetSSID(String ssid){
		mWifiItem.ssid = ssid;
	}

	public void SetProtect(String protect){
		mWifiItem.protect = protect;
	}
	
	public void SetPassword(String password){
		mWifiItem.password = password;
	}
	
	public String GetSSID(){
		return mWifiItem.ssid;
	}

	public String GetProtect(){
		return mWifiItem.protect;
	}
	
	public String GetPassword(){
		return mWifiItem.password;
	}
	
	public void SetSSIDEnable(boolean enable){
		mSsidEnable = enable;
	}

	public void SetProtectEnable(boolean enable){
		mProtectEnable = enable;
	}
	
	public void SetPasswordEnable(boolean enable){
		mPasswordEenable = enable;
	}	
	
	private int GetProtectIndex(String protect) {
        for(int index=0;index<protect_str.length;index++) {
        	if(protect_str[index].equals(protect)) {
        		return index;
        	}
        }		
        return 0;
	}
	
	public void show() {

		LayoutInflater factory = LayoutInflater.from(mContext);
        View textEntryView = factory.inflate(R.layout.net_input_dlg,null);
        
        
        final EditText dlg_editTextSSID = (EditText) textEntryView.findViewById(R.id.editTextSSID);	
        final EditText dlg_editTextPassword = (EditText) textEntryView.findViewById(R.id.editTextPassword);	
        final Spinner dlg_spinder = (Spinner) textEntryView.findViewById(R.id.spinnerProtect);

        ArrayAdapter<String> dlg_adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_spinner_item,
        		protect_str);        

        AlertDialog.Builder dlg = new AlertDialog.Builder(mContext) {
        	public AlertDialog create() {
                dlg_editTextSSID.setText(mWifiItem.ssid);
                dlg_editTextPassword.setText(mWifiItem.password);
                dlg_spinder.setSelection(GetProtectIndex(mWifiItem.protect));   
                return super.create();
        	}
        };       
           
        dlg_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dlg_spinder.setAdapter(dlg_adapter);
        dlg.setView(textEntryView);
        
        dlg_spinder.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
        });
        
        dlg.setPositiveButton("OK", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which) {
				mWifiItem.ssid = dlg_editTextSSID.getText().toString();
				mWifiItem.password = dlg_editTextPassword.getText().toString();
				mWifiItem.protect = dlg_spinder.getSelectedItem().toString();    
				Result(mWifiItem);
			}
		});	
        
        dlg.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which) {
				Result(null);
			}
		});	
        
        dlg.setTitle("WIFI configuration");

        dlg_editTextSSID.setEnabled(mSsidEnable);
        dlg_editTextPassword.setEnabled(mPasswordEenable);
        dlg_spinder.setEnabled(mProtectEnable);
        
		AlertDialog alertDialog = dlg.create();			       
		alertDialog.show();			
	}
	
	public void Result(NmpWifiInfo wifiinfo) {

	}	
}
